﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server;

namespace NetBay.Core.BusinessEntities.Workflow
{
    [DataContract]
    public class WorkflowInstance : ReturnObject
    {
        #region Attributs
        private List<WorkflowItem> _WorkItems = new List<WorkflowItem>();
        private WorkflowDefinition _Definitions = new WorkflowDefinition();
        private List<WorkflowInstance> _Childs = new List<WorkflowInstance>(); 

        private User _Sender = new User();
        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the identifier.
        /// </summary>
        /// <value>The identifier.</value>
        [DataMember]
        public long Identifier { get; set; }

        /// <summary>
        /// Gets or sets the parent id.
        /// </summary>
        /// <value>The parent id.</value>
        [DataMember]
        public long ParentId { get; set; }

        /// <summary>
        /// Gets or sets the unique group instance id.
        /// </summary>
        /// <value>
        /// The unique group instance id.
        /// </value>
        [DataMember]
        public long UniqueGroupInstanceId { get; set; }

        /// <summary>
        /// Gets or sets the definition id.
        /// </summary>
        /// <value>The definition id.</value>
        [DataMember]
        public long DefinitionId { get; set; }

        /// <summary>
        /// Gets or sets the name of the definition.
        /// </summary>
        /// <value>The definition id.</value>
        [DataMember]
        public string DefinitionName { get; set; }

        /// <summary>
        /// Gets or sets the definition.
        /// </summary>
        /// <value>The definition.</value>
        [Association("FK_Definition", "ForeignKey", "Key")]
        [Include]
        [DataMember]
        public WorkflowDefinition Definition
        {
            get
            {
                return _Definitions;
            }
            set
            {
                _Definitions = value;
                ForeignKey = _Definitions.Key;
            }
        }

        /// <summary>
        /// Gets or sets the folder id.
        /// </summary>
        /// <value>The folder id.</value>
        [DataMember]
        public long FolderId { get; set; }

        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        /// <value>The state.</value>
        [DataMember]
        public WorkflowState State { get; set; }

        /// <summary>
        /// Gets or sets the state label.
        /// </summary>
        /// <value>The state label.</value>
        [DataMember]
        public string StateLabel { get; set; }

        /// <summary>
        /// Gets or sets the starting condition.
        /// </summary>
        /// <value>The starting condition.</value>
        [DataMember]
        public long StartingCondition { get; set; }

        /// <summary>
        /// Gets or sets the name of the starting condition.
        /// </summary>
        /// <value>The starting condition.</value>
        [DataMember]
        public string StartingConditionLabel { get; set; }

        /// <summary>
        /// Gets or sets the sender.
        /// </summary>
        /// <value>The sender.</value>
        [Association("FK_Sender", "ForeignKey1", "Key")]
        [Include]
        [DataMember]
        public User Sender
        {
            get { return _Sender; }
            set
            {
                ForeignKey1 = value.Key;
                _Sender = value;
            }
        }

        /// <summary>
        /// Gets or sets the user name that create the instance.
        /// </summary>
        /// <value>The user name.</value>
        [DataMember]
        public string SendedBy { get; set; }

        /// <summary>
        /// Gets or sets the created on.
        /// </summary>
        /// <value>The created on.</value>
        [DataMember]
        public DateTime CreatedOn { get; set; }

        /// <summary>
        /// Gets or sets the chrono id.
        /// </summary>
        /// <value>The chrono id.</value>
        [DataMember]
        public Nullable<long> ChronoId { get; set; }

        /// <summary>
        /// Gets or sets the is ended.
        /// </summary>
        /// <value>The is ended.</value>
        [DataMember]
        public bool IsEnded { get; set; }

        /// <summary>
        /// Gets or sets the receivers.
        /// </summary>
        /// <value>The receivers.</value>
        [DataMember]
        public string Receivers { get; set; }

        /// <summary>
        /// Gets or sets the group user id.
        /// </summary>
        /// <value>The group user id.</value>
        [DataMember]
        public long GroupUserId { get; set; }

        /// <summary>
        /// Gets or sets the group id.
        /// </summary>
        /// <value>The group id.</value>
        [DataMember]
        public long GroupId { get; set; }

        /// <summary>
        /// Gets or sets the group id.
        /// </summary>
        /// <value>The group id.</value>
        [DataMember]
        public string GroupName { get; set; }

        /// <summary>
        /// Gets or sets the rgu id.
        /// </summary>
        /// <value>The rgu id.</value>
        [DataMember]
        public long RguId { get; set; }

        /// <summary>
        /// Gets or sets my doc etat.
        /// </summary>
        /// <value>My doc etat.</value>
        [DataMember]
        public bool IsRead { get; set; }

        /// <summary>
        /// Gets or sets the has child.
        /// </summary>
        /// <value>The has child.</value>
        [DataMember]
        public bool HasChild { get; set; }

        /// <summary>
        /// Gets or sets the instance user id.
        /// </summary>
        /// <value>The instance user id.</value>
        [DataMember]
        public long InstanceUserId { get; set; }

        /// <summary>
        /// Get or set the last message read date
        /// </summary>
        /// <value>The last read date.</value>
        [DataMember]
        public DateTime LastReadDate { get; set; }

        /// <summary>
        /// Get or set the last message action date
        /// </summary>
        /// <value>The last action date.</value>
        [DataMember]
        public DateTime LastActionDate { get; set; }

        /// <summary>
        /// Gets or sets the state of the documents.
        /// </summary>
        /// <value>The state of the documents.</value>
        [Association("FK_DocumentsState", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public List<WorkflowItem> WorkItems
        {
            get
            {
                return _WorkItems;
            }
            set
            {
                _WorkItems = value;
                ItemEntity.SetForeignKey(_WorkItems, Key);
            }
        }

        /// <summary>
        /// Gets or sets the state of the documents.
        /// </summary>
        /// <value>The state of the documents.</value>
        [Association("FK_Childs", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public List<WorkflowInstance> Childs
        {
            get
            {
                return _Childs;
            }
            set
            {
                _Childs = value;
                ItemEntity.SetForeignKey(_Childs, Key);
            }
        }

        /// <summary>
        /// Gets or sets the index of the folder.
        /// </summary>
        /// <value>
        /// The index of the folder.
        /// </value>
        [DataMember]
        public Dictionary<string, string> FolderIndex { get; set; }
 

        #endregion

        #region Public Functions

        public override object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowInstance" /> class.
        /// </summary>
        public WorkflowInstance()
            : base()
        {
            FolderIndex = new Dictionary<string, string>();
        }

        /// <summary>
        /// Only for error
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="applicationException">The application exception.</param>
        public WorkflowInstance(string errorMessage, Exception applicationException = null)
            : base(errorMessage, applicationException)
        {
            FolderIndex = new Dictionary<string, string>();
        }
        #endregion
    }
}